﻿using System;
using System.Collections.Generic;
using System.Text;
using AgencyManagerSystem.Model;
using AgencyManagerSystem.DataUpdater;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.DataFetcher;

namespace AgencyManagerSystem.Core
{
    public class CustomerUpdaterCreator : BaseUpdaterCreator<CustomerModel>
    {
        public CustomerUpdater CreateCustomerUpdaterInstance()
        {
            CustomerUpdater customerUpdater = new CustomerUpdater(new CustomerFetcher());
            customerUpdater.DataUpdating += FlagAndApproveFlagValid;
            customerUpdater.DataUpdating += DataNotSpace;
            customerUpdater.DataUpdating += CodeUnique;
            customerUpdater.DataUpdating += RetailExist;
            customerUpdater.DataUpdating += SalesExist;
            customerUpdater.DataDeleting += OrderExist;
            return customerUpdater;
        }

        private void SalesExist(BaseUpdater<CustomerModel> sender, DataUpdatingEventArgs<CustomerModel> e)
        {
            if (e.Insert == null) return;
            var userFetcher = new UserFetcher();
            var user = userFetcher.Get(e.Insert.Sales);
            if (user == null)
            {
                e.Message = String.Format("导购{0}信息不存在", e.Insert.Retail);
                e.Cancel = true;
                return;
            }
            if(!user.Retail)
            {
                e.Message = String.Format("用户{0}没有导购属性", e.Insert.Retail);
                e.Cancel = true;
                return;
            }
            var retailMemberFetcher = new RetailMemberFetcher();
            var retailMember = retailMemberFetcher.GetByUser(e.Insert.Sales);
            if(retailMember==null||retailMember.Retail!=e.Insert.AutoId)
            {
                e.Message = String.Format("用户{0}不属于经销商{1}",e.Insert.Sales, e.Insert.Retail);
                e.Cancel = true;
                return;
            }
        }

        private void OrderExist(BaseUpdater<CustomerModel> sender, DataDeletingEventArgs<CustomerModel> e)
        {
            if (e.Delete == null) return;
            var orderFetcher = new OrderFetcher();
            var orderList = orderFetcher.List(new String[] { "Customer="+ e.Delete.AutoId.ToString()  });
            if(orderList.Count>0)
            {
                e.Message = String.Format("客户信息已存在于订单{0}等{1}笔订单记录", orderList[0].Code, orderList.Count);
                e.Cancel = true;
                return;
            }
        }

        private void RetailExist(BaseUpdater<CustomerModel> sender, DataUpdatingEventArgs<CustomerModel> e)
        {
            if (e.Insert == null) return;
            var retailFetcher = new RetailFetcher();
            var retail = retailFetcher.Get(e.Insert.Retail);
            if(retail==null)
            {
                e.Message = String.Format("经销商{0}信息不存在", e.Insert.Retail);
                e.Cancel = true;
                return;
            }
        }

        private void CodeUnique(BaseUpdater<CustomerModel> sender, DataUpdatingEventArgs<CustomerModel> e)
        {
            if (e.Insert == null) return;
            var customerFetcher = new CustomerFetcher();
            var customer = customerFetcher.GetByCode(e.Insert.Retail, e.Insert.Code);
            if(customer!=null&&customer.AutoId!= e.Insert.AutoId)
            {
                e.Message = String.Format("编号{0}已经存在于客户{1}", e.Insert.Code, customer.AutoId);
                e.Cancel = true;
                return;
            }
        }

        private void DataNotSpace(BaseUpdater<CustomerModel> sender, DataUpdatingEventArgs<CustomerModel> e)
        {
            if (e.Insert == null) return;
            if(String.IsNullOrWhiteSpace(e.Insert.Code)
                ||String.IsNullOrWhiteSpace(e.Insert.Name)
                || String.IsNullOrWhiteSpace(e.Insert.Tel))
            {
                e.Cancel = true;
                e.Message = "编号，姓名及联系方式不能为空";
                return;
            }
        }
    }
}
